package com.tiecode.plugin.regex.editor.decision;

import android.util.ArrayMap;

import com.tiecode.plugin.regex.action.RegexCodeBodyAction;
import com.tiecode.plugin.regex.editor.RegexEntity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import io.github.rosemoe.sora.widget.schemes.EditorColorScheme;
import io.github.scave.lsp4a.highlight.decision.DecisionResult;
import io.github.scave.lsp4a.highlight.token.IdentifierToken;
import io.github.scave.lsp4a.highlight.token.TokenModel;

public class RegexDecision extends AbstractDecision {

    private static final Map<Integer, RegexEntity> regexEntityMap = new ArrayMap<>();

    @Override
    public Iterable<DecisionResult> decide(TokenModel model, State state) {
        if (model.line == state.testLine) {
            return singleColorBold(EditorColorScheme.KEYWORD);
        }
        List<DecisionResult> results = new ArrayList<>();
        if (model.token == IdentifierToken.SHARP) {
            if (model.prev == null || getPrevNonEmptyToken(model).token == IdentifierToken.NEWLINE) {
                state.testLine = model.line;
                RegexEntity requestEntity = parseRegexEntity(model);
                regexEntityMap.put(model.line, requestEntity);

                DecisionResult result = new DecisionResult();
                result.kind = DecisionResult.ResultKind.ICON;
                result.content = new DecisionResult.IconContent(RegexCodeBodyAction.testDrawable);
                results.add(result);

                DecisionResult color = new DecisionResult(
                        DecisionResult.ResultKind.ACCEPT,
                        new DecisionResult.StyleContent(EditorColorScheme.KEYWORD, true, false)
                );
                results.add(color);
            }
        } else if (model.line == state.testLine + 1) {
            return singleColor(EditorColorScheme.STRING);
        }
        return results;
    }

    private TokenModel model;

    private RegexEntity parseRegexEntity(TokenModel model) {
        this.model = model;
        RegexEntity entity = new RegexEntity();
        //解析正则测试名称
        parseRegexName(entity);
        //解析表达式
        parseRegexPattern(entity);
        //解析测试内容
        parseRegexContent(entity);
        return entity;
    }

    private void parseRegexName(RegexEntity entity) {
        model = model.next;
        String testName = "";
        while (model != null) {
            if (model.token == IdentifierToken.NEWLINE) {
                model = model.next;
                break;
            }
            testName += model.text;
            model = model.next;
        }
        entity.name = testName.trim();
    }

    private void parseRegexPattern(RegexEntity entity) {
        if (model == null) {
            return;
        }
        String regex = "";
        while (model != null) {
            if (model.token == IdentifierToken.NEWLINE) {
                model = model.next;
                break;
            }
            regex += model.text;
            model = model.next;
        }
        entity.regex = regex;
    }

    private void parseRegexContent(RegexEntity entity) {
        if (model == null) {
            return;
        }
        String content = "";
        while (model != null) {
            if (model.token == IdentifierToken.SHARP || model.token == IdentifierToken.EOF) {
                break;
            }
            content += model.text;
            model = model.next;
        }
        entity.content = content;
    }

    public static RegexEntity getRequestEntity(int line) {
        return regexEntityMap.get(line);
    }

    public static Collection<RegexEntity> getRequestEntities() {
        return regexEntityMap.values();
    }

    public static void clear() {
        regexEntityMap.clear();
    }
}
